/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.lcm.metadata.devcommon;

import com.inspur.edp.lcm.metadata.common.FileServiceImp;
import com.inspur.edp.lcm.metadata.common.configuration.WorkspaceHelper;

public class ManagerUtils {
	public static final String Spring_Shell_Flag_Name = "spring-shell";
	public static final String Spring_Shell_Flag_True = "1";
	public static final boolean Shell_Flag = Spring_Shell_Flag_True.equals(System.getProperty(Spring_Shell_Flag_Name));
	private static final FileServiceImp fileService = new FileServiceImp();
	/**
	 * 获取开发根路径
	 */
	private static String devRootPath;
	private static String mavenStoragePath;
	private static String metadataPackageLocation;

	public static String getDevRootPath() {
		if (Shell_Flag) {
			return devRootPath;
		}
		return WorkspaceHelper.getInstance().getDevRootPath();
	}

	public static void setDevRootPath(String path) {
		if (Shell_Flag) {
			devRootPath = path;
		}

	}

	public static String getMetadataPackageLocation() {
		if (Shell_Flag) {
			return metadataPackageLocation;
		}
		return fileService.getCombinePath(getDevRootPath(), "packages");
	}

	public static void setMetadataPackageLocation(String metadataPackageLoc) {
		if (Shell_Flag) {
			metadataPackageLocation = metadataPackageLoc;
		}
	}

	/**
	 * @param path 获取绝对路径
	 * @return
	 */
	public static String getAbsolutePath(String path) {
		path = handlePath(path);
		if (Shell_Flag) {
			if(isAbsolutePath(path)){
				return path;
			}else {
				throw new RuntimeException("当前路径不是绝对路径，请确认。当前路径为："+path);
			}

		}
		String devRootPath = handlePath(getDevRootPath());
		if (path.startsWith(devRootPath)) {
			if(isAbsolutePath(path)){
				return path;
			}else {
				throw new RuntimeException("当前路径不是绝对路径，请确认。当前路径为："+path);
			}
		} else if(path.startsWith("/")||path.startsWith("\\")){
			path=path.substring(1);
		}
		if (path.startsWith("/")) {
			path = path.substring(1);
		}
		String absolutePath = fileService.getCombinePath(devRootPath, path);
		// TODO 判断路径是否存在
		return handlePath(absolutePath);
	}

	public static boolean isAbsolutePath(String path) {
		return handlePath(path).startsWith("/")||path.contains(":/");
	}

	public static String handlePath(String path) {
		return path.replace("\\", "/");
	}

	public static String getMavenStoragePath() {
		if (Shell_Flag) {
			return mavenStoragePath;
		}
		return fileService.getCombinePath(getDevRootPath(), "maven");
	}

	public static void setMavenStoragePath(String mavenStorageP) {
		if (Shell_Flag) {
			mavenStoragePath = mavenStorageP;
		}
	}

	@Deprecated
	public static String getRalativePath(String relativeFullPath) {
		return getRelativePath(relativeFullPath);
	}

	public static String getRelativePath(String relativeFullPath) {
		String relativePath;
		if (!getDevRootPath().isEmpty() && fileService.startWith(relativeFullPath, getDevRootPath())) {
			relativePath = FileServiceImp.getRelativePath(relativeFullPath, getDevRootPath());
		} else {
			relativePath = relativeFullPath;
		}
		return relativePath;
	}
}
